package com.tencent.wxcloudrun.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.entity.UserAllInfo;
import com.tencent.wxcloudrun.entity.UserInfo;
import com.tencent.wxcloudrun.entity.UserInfoInformation;
import com.tencent.wxcloudrun.init.AccessToken;
import com.tencent.wxcloudrun.mapper.UserMapper;
import com.tencent.wxcloudrun.service.UserService;
import com.tencent.wxcloudrun.vo.FinalClass;
import com.tencent.wxcloudrun.vo.GetAPIInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<UserAllInfo> getUserList() throws Exception {
        return userMapper.getUserList();
    }

    //获取用户手机号
    //accessToken 后台唯一编码
    //code 用户code
    @Override
    public JSONObject getUserPhone(String code){
        GetAPIInfo getAPIInfo = new GetAPIInfo();
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+ AccessToken.accessToken;
        Map<String,String> map = new HashMap<>();
        map.put("code",code);
        JSONObject jsonObject = getAPIInfo.postAPIInfo(url,map);
        return jsonObject;
    }
   //用户登录
    @Override
    public JSONObject userLogin(String code){
        GetAPIInfo getAPIInfo = new GetAPIInfo();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+ FinalClass.getAppId() +"&secret="+FinalClass.getAppSecret()+"&js_code="+code+"&grant_type=authorization_code";
        JSONObject jsonObject = getAPIInfo.getAPIInfo(url);
        return jsonObject;
    }


    //录入用户session以及code
    @Override
    public int insertUserSessionKey(UserInfo userInfo){
        return userMapper.insertUserSessionKey(userInfo);
    }

    //查询数据库内是否有此用户手机号
    @Override
    public int queryUserPhone(String userPhone){
        return userMapper.queryUserPhone(userPhone);
    }
    //更新用户的sessionkey和code
    @Override
    public int updateUserSessionAndCode(UserInfo userInfo){
        return userMapper.updateUserSessionAndCode(userInfo);
    }
    //根据用户手机号获取其他信息
    @Override
    public UserAllInfo getUser(String phone){
        return userMapper.getUser(phone);
    }
    //根据userInfoId将用户其他信息保存
    @Override
    public int insertUserInfo(UserInfoInformation userInfoInformation){
        return userMapper.insertUserInfo(userInfoInformation);
    }

    //获取该用户的id
    @Override
    public int getUserId(String phoneNumber){
        return userMapper.getUserId(phoneNumber);
    }

    //查询userinfoinformation表中是否含有此uid
    @Override
    public int queryUserInfoInformationUid(int uid){
        return userMapper.queryUserInfoInformationUid(uid);
    }

    //更新用户详细信息表
    @Override
    public int updateUserInformation(UserInfoInformation userInfoInformation) {
        return userMapper.updateUserInformation(userInfoInformation);
    }

}
